package com.KOL.model;

import java.awt.Point;
import java.awt.*;
import java.awt.image.*;

import com.util.ImagesLoader;
import com.util.ImagesPlayer;

public class Entity {

    private static final int XSTEP = 5;
    private static final int YSTEP = 5;
    private static final int SIZE = 12;

    private ImagesLoader imsLoader;
    private String imageName;
    private String name;
    private BufferedImage image;
    private int width, height;

    private ImagesPlayer player;
    private boolean isLooping;

    private int pWidth, pHeight;

    private boolean isActive = true;
    protected int locx, locy;
    protected int dx, dy;

    public Entity(int x, int y, int w, int h, ImagesLoader imsLd, String name) {
        locx = x; locy = y;
        pWidth = w; pHeight = h;
        dx = XSTEP; dy = YSTEP;
        this.name = name;
        imsLoader = imsLd;
        setImage(name);
    }

    public void setImage(String name) {
        imageName = name;
        image = imsLoader.getImage(imageName);

        if (image == null) {
            System.err.println("No sprite image for " + imageName);
            width = SIZE;
            height = SIZE;
        }
        else {
            width = image.getWidth();
            height = image.getHeight();
        }

        player = null;
        isLooping = false;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    public BufferedImage getImage() {
        if (isLooping)
            return player.getCurrentImage();
        else
            return image;
    }

    public void loopImage(int animPeriod, double seqDuration) {
        if (imsLoader.numImages(imageName) > 1) {
            player = null;
            player = new ImagesPlayer(imageName, animPeriod, seqDuration, true, imsLoader);
            isLooping = true;
        } else
            System.err.println(imageName + " is not a sequence of images");
    }

    public void stopLooping() {
        if (isLooping) {
            player.stop();
            isLooping = false;
        }
    }

    public int getWidth()
    {  return width;  }

    public int getHeight()
    {  return height;  }

    public int getPWidth()
    {  return pWidth;  }

    public int getPHeight()
    {  return pHeight;  }


    public boolean isActive()
    {  return isActive;  }

    public void setActive(boolean a)
    {  isActive = a;  }

    public void setPosition(int x, int y)
    {  locx = x; locy = y;  }

    public void translate(int xDist, int yDist)
    {  locx += xDist;  locy += yDist;  }

    public int getXPosn()
    {  return locx;  }

    public int getYPosn()
    {  return locy;  }

    public void setStep(int dx, int dy)
    {  this.dx = dx; this.dy = dy; }

    public int getXStep()
    {  return dx;  }

    public int getYStep()
    {  return dy;  }

    public Rectangle getMyRectangle()
    {  return  new Rectangle(locx, locy, width, height);  }

    protected ImagesLoader getImagesLoader() {
        return imsLoader;
    }

    public void updateSprite() {
        if (isActive()) {
            locx += dx;
            locy += dy;

            if (isLooping)
                player.updateTick();
        }
    }

    public void drawSprite(Graphics g) {
        if (isActive()) {
            if (image == null) {
                //TODO handle differently for null image case
                g.setColor(Color.yellow);
                g.fillOval(locx, locy, SIZE, SIZE);
                g.setColor(Color.black);
            } else {
                if (isLooping)
                    image = player.getCurrentImage();
                g.drawImage(image, locx, locy, null);
            }
        }
    }
}

